package com.jdcjk.project.main.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.jdcjk.R;

/**
 * Created by Administrator on 2017/3/21 0021.
 */

public class OpenGpsDialog extends ProgressDialog {

    private Context context;
    //取消打开gps功能button
    private Button openGpsCancleBtn;
    //打开gps功能button
    private Button openGpsBtn;

    //点击事件回调接口
    private ClickListenerInterface clickListenerInterface;
    private View mView;

    public interface ClickListenerInterface {
        void gpsCancle();

        void gpsOpen();
    }

    public void setClickListenerInterface(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    public OpenGpsDialog(Context context) {
        this(context, R.style.dialogTheme);
    }

    public OpenGpsDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
        // 初始化
        init();
        // 设置触摸外部界面不会使dialog消失
        this.setCanceledOnTouchOutside(false);
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        mView = inflater.inflate(R.layout.dialog_open_gps, null);
        openGpsCancleBtn = (Button) mView.findViewById(R.id.dialog_open_gps_cancle_btn);
        openGpsBtn = (Button) mView.findViewById(R.id.dialog_open_gps_btn);
        openGpsCancleBtn.setOnClickListener(new clickListener());
        openGpsBtn.setOnClickListener(new clickListener());
    }

    @Override
    public void show() {
        super.show();
        this.setContentView(mView);
        /*
         * 将对话框的大小按屏幕大小的百分比设置(必须在setContentView之后设置才有效)
         */
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
        lp.width = (int) (d.widthPixels * 0.9); // 宽度设置为屏幕的0.8
        dialogWindow.setAttributes(lp);
    }

    private class clickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.dialog_open_gps_cancle_btn:
                    clickListenerInterface.gpsCancle();
                    break;
                case R.id.dialog_open_gps_btn:
                    clickListenerInterface.gpsOpen();
                    break;
            }
        }
    }

    ;

    @Override
    public void cancel() {
        super.cancel();
    }
}
